package com.org.gundutubruk;
import com.org.gundutubruk.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

/*
 * UI buat client milih player apa yang akan dipake buat main di server(mirip kayak remove player, 
 * sama ada tambahan data gundu dia berapa) 
 * buat ke activity berikunya panggil lanjutKeRoom()
 */

public class SelectClientPlayerActivity extends Activity{

	SelectClientPlayerActivity self;
	
	int mType;
	
	//variabel yang perlu diisi
	String namaPlayer;
	int jumGundu;
	int avatar;
	
	Button kanan;
	Button kiri;
	Button lanjut;
	Button tambah;
	Button kurang;
	TextView namaP;
	TextView gunduP;
	ImageView dispAvatar;
	
	int currP;
	int gbrApatar;
	int gbrNoPlayer;
	
	public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);    	
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		//ambil data apakah dia player atau server
		Intent startingIntent = getIntent();
		mType = startingIntent.getIntExtra("TYPE", 0);
				
		self = this;
		setContentView(R.layout.select_client_layout);
		gbrApatar=R.drawable.apatar0;
    	gbrNoPlayer=R.drawable.apatar;
		castitem();
		init();
		isifungsiitem();
	}
	
	public void castitem(){
		kanan=(Button)findViewById(R.id.playerplus);
		kiri=(Button)findViewById(R.id.playermin);
		lanjut=(Button)findViewById(R.id.tmblmulai);
		namaP=(TextView)findViewById(R.id.namaP);
		gunduP=(TextView)findViewById(R.id.gunduP);
		dispAvatar=(ImageView)findViewById(R.id.gbrapatar);
		tambah=(Button)findViewById(R.id.pluspemain);
		kurang=(Button)findViewById(R.id.minuspemain);
	}
	
	private void init(){
		if(Utility.playerHolder.size()>0){
			currP=0;
			dispPlayerInfo();
		}else{
			currP=-1;
			namaP.setText("---");
			gunduP.setText("---");
			dispAvatar.setImageResource(gbrNoPlayer);
		}
	}

	private void dispPlayerInfo() {
		Player P = Utility.playerHolder.get(currP);
		Log.d("", "nama : "+P.name);
		namaP.setText(P.name);
		gunduP.setText(""+P.jumlahGundu);
		//Log.d("", "angka : "+(gbrApatar+P.avatar));
		dispAvatar.setImageResource(gbrApatar+P.avatar);
	}
	
	public void isifungsiitem(){
		kiri.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if(currP>0){
					currP--;
					dispPlayerInfo();
				}
			}
		});
		kanan.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if(currP<Utility.playerHolder.size()-1){
					currP++;
					dispPlayerInfo();
				}
			}
		});
		lanjut.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				if(currP>-1){
					Player P = Utility.playerHolder.get(currP);
					namaPlayer=P.name;
					jumGundu=P.jumlahGundu;
					avatar=P.avatar;
					lanjutKeRoom();
				}
			}
		});
		tambah.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				lanjutKeTambahPemain();
			}
		});
		kurang.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				lanjutKeHapusPemain();
			}
		});
	}
	
	public void onBackPressed(){
		overridePendingTransition(R.anim.fade, R.anim.hold);
		this.finish();
	}
	
	public void lanjutKeRoom() {
		Intent intent;	     
		if(mType==0) {
			intent = new Intent(self, SelectServerGameActivity.class);
			intent.putExtra("TYPE", 0);//sebagai server			 
		}else{
			intent = new Intent(self, RoomMultiPlayer.class);
			intent.putExtra("TYPE",1);
		}
    	intent.putExtra("namaPlayer", namaPlayer);
    	intent.putExtra("jumGundu", jumGundu);
    	intent.putExtra("avatar", avatar);
        startActivity(intent);
		overridePendingTransition(R.anim.fade, R.anim.hold);
		this.finish();
	}
	
	public void lanjutKeTambahPemain() {
		Intent intent = new Intent(self,NewPlayer.class);
		intent.putExtra("fromMultiplayer", true);
		startActivity(intent);
		overridePendingTransition(R.anim.fade, R.anim.hold);		
	}
	
	public void lanjutKeHapusPemain() {
		Intent intent = new Intent(self,RemovePlayer.class);
		intent.putExtra("fromMultiplayer", true);
		startActivity(intent);
		overridePendingTransition(R.anim.fade, R.anim.hold);		
	}
}
